import { accAdd, accMultiply } from "../utils/number"

class Caculator {
    totalPrice = 0
    totalSkuCount = 0
    cartItems = []

    constructor(cartItems) {
        this.cartItems = cartItems
        // this.init(cartItems)
    }
    getTotalPrice() {
        return this.totalPrice
    }

    getTotalSkuCount() {
        return this.totalSkuCount
    }


    calc() {
        this.cartItems.forEach(cartItem => {
            this.push(cartItem)
        });
    }

    push(cartItem) {
        let partTotalPrice = 0
        if (cartItem.sku.discount_price) {
            partTotalPrice = accMultiply(cartItem.count, cartItem.sku.discount_price)
        } else {
            partTotalPrice = accMultiply(cartItem.count, cartItem.sku.price)

        }
        this.totalPrice = accAdd(this.totalPrice, partTotalPrice)
        this.totalSkuCount = accAdd(cartItem.count, this.totalSkuCount)
    }
}

export {
    Caculator
}